//
//  MapSelectViewController.swift
//  TJJPT
//
//  Created by FENGYL on 2017/7/3.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit

class MapSelectViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var selectMapTableView:UITableView?
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setUpView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // MARK: - 初始化
    func setUpView() {
        
        self.title = NSLocalizedString(LOCALIZABLE_STRING_MAP_SELECT, comment: "")
        
        
        selectMapTableView = UITableView.init(frame: self.view.frame)
        
        selectMapTableView?.tableFooterView = UIView.init()
        
        selectMapTableView?.delegate = self
        selectMapTableView?.dataSource = self
        
        self.view.addSubview(selectMapTableView!)
        
    }
    
    // MARK: - 设置tableview
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 2
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        
        if cell == nil {
            
            cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: "cell")
            
        }
        
        cell?.selectionStyle = .none
        
        var isBaiduMap:Bool?
        
        
        if UserModel.userModel.selectedMap == LOCALIZABLE_STRING_BAIDU_MAP {
            
            isBaiduMap = true
            
        }else {
            
            isBaiduMap = false
            
        }
        
        if indexPath.row == 0 {
            
            cell?.textLabel?.text = NSLocalizedString(LOCALIZABLE_STRING_BAIDU_MAP, comment: "")
            
            if isBaiduMap! {
                
                cell?.imageView?.image = #imageLiteral(resourceName: "z_gps_command_yuan2_45x45")
                
            }else {
                
                cell?.imageView?.image = #imageLiteral(resourceName: "z_gps_command_yuan1_45x45")
                
            }
            
        }else if indexPath.row == 1{
            
            cell?.textLabel?.text = NSLocalizedString(LOCALIZABLE_STRING_AMAP, comment: "")
            
            if !isBaiduMap! {
                
                cell?.imageView?.image = #imageLiteral(resourceName: "z_gps_command_yuan2_45x45")
                
            }else {
                
                cell?.imageView?.image = #imageLiteral(resourceName: "z_gps_command_yuan1_45x45")
                
            }
            
        }
        
        
        
        
        return cell!
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        var selectMap:String?
        
        if indexPath.row == 0 {
            
            selectMap = LOCALIZABLE_STRING_BAIDU_MAP
            
        }else if indexPath.row == 1 {
            
            selectMap = LOCALIZABLE_STRING_AMAP
            
        }
        
        UserDefaults.standard.set(selectMap, forKey: USER_DEFAULTS_SELECTED_MAP)
        
        UserModel.userModel.selectedMap = selectMap
        
        print(selectMap!)
        
        selectMapTableView?.reloadData()
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
